package com.itheima.ydd.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.ydd.common.PageResult;
import com.itheima.ydd.common.ResultData;
import com.itheima.ydd.entity.Article;
import com.itheima.ydd.entity.YddUser;
import com.itheima.ydd.service.ArticleService;
import com.itheima.ydd.service.YddUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * 会员管理（后台）用户管理
 */
@RestController
@Slf4j
@RequestMapping("ydd_user")
public class YddUserController {
    @Autowired
    private YddUserService yddUserService;

    @Autowired
    private ArticleService articleService;

    /**
     * 返回用户详情
     *
     * @param id
     * @return
     */
    @GetMapping("rowInfo")
    public ResultData rowInfo(Integer id) {
        log.info("返回用户详情id:{}", id);
        YddUser yddUser = yddUserService.getById(id);
        return ResultData.ok("", yddUser);
    }

    /**
     * 用户列表-分页
     *
     * @param sex
     * @param username
     * @param mobile
     * @param page
     * @param limit
     * @return
     */
    @GetMapping("list")
    public ResultData list(String sex, String username, String mobile, Integer page, Integer limit) {
        log.info("用户列表-分页sex:{},username:{},mobile:{},页码page:{},每页数量limit:{}", sex, username, mobile, page, limit);
        HashMap<String, Object> map = yddUserService.getPageHashMap(sex, username, mobile, page, limit);
        return ResultData.ok("", map);
    }

    /**
     * 用户删除
     *
     * @param id
     * @return
     */
    @GetMapping("del")
    public ResultData del(Integer id) {
        log.info("用户删除id:{}", id);
        yddUserService.removeById(id);
        return ResultData.ok("", "用户删除成功");
    }

    /**
     * 用户修改
     *
     * @param yddUser
     * @return
     */
    @PostMapping("update")
    public ResultData update(@RequestBody YddUser yddUser) {
        log.info("用户修改YddUser:{}", yddUser);
        yddUserService.updateById(yddUser);
        return ResultData.ok("", "用户修改成功");
    }

    /**
     * 用户添加
     *
     * @param yddUser
     * @return
     */
    @PostMapping("add")
    public ResultData add(@RequestBody YddUser yddUser) {
        log.info("分类添加YddUser:{}", yddUser);
        yddUser.setDefault_data("1");
        yddUserService.save(yddUser);
        return ResultData.ok("", "分类添加成功");
    }


}
